package com.meihua.baoleyi.ui;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.dialog.ProductInfoPop;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.pay.WxPay;
import com.meihua.baoleyi.widget.AlertDialog;
import com.meihua.baoleyi.widget.CustomerKeyboard;
import com.meihua.baoleyi.widget.PasswordEditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 账户充值界面
 */
public class AccountRechargeActivity extends BaseActivity implements View.OnClickListener, CustomerKeyboard.CustomerKeyboardClickListener, PasswordEditText.PasswordFullListener {
    @Bind(R.id.text_back)
    ImageView textBack;
    @Bind(R.id.text_title)
    TextView textTitle;
    @Bind(R.id.top_right_tv)
    TextView topRightTv;
    @Bind(R.id.iv_head)
    ImageView ivHead;
    @Bind(R.id.tv_bankname)
    TextView tvBankname;
    @Bind(R.id.tv_balance)
    TextView tvBalance;
    @Bind(R.id.rl_bank)
    RelativeLayout rlBank;
    @Bind(R.id.tv_jine)
    TextView tvJine;
    @Bind(R.id.et_money)
    EditText etMoney;
    @Bind(R.id.btn_next)
    Button btnNext;

    private ProductInfoPop productInfo;
    private AlertDialog dialog;
    private String mMoney; //提现金额

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhang_hu_chong_zhi);
        ButterKnife.bind(this);
        textTitle.setText("账户充值");
    }

    @OnClick({R.id.rl_bank, R.id.btn_next, R.id.text_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_bank:
                /*暂时只支持微信支付*/
               /* showProductPop("余额充值");*/
                break;
            case R.id.text_back://返回
                finish();
                break;
            case R.id.btn_next: //下一步 充值
                mMoney = etMoney.getText().toString().trim();
                if (mMoney.equals("") && mMoney.isEmpty()) {
                    Toast.makeText(AccountRechargeActivity.this, "请输入充值金额", Toast.LENGTH_SHORT).show();
                    return;
                }
                /*弹出输入密码对话框*/
                confirmPay();
                break;
        }
    }
    private PasswordEditText passwordEditText;
    private CustomerKeyboard mCustomerKeyboard;

    /**
     * 弹出输入密码框
     */
    private void showCustomInputDialog() {

        dialog = new AlertDialog.Builder(this).
                setContentView(R.layout.activity_password).
                fullWith().
                setCancelable(true).
                formBottom(true).
                show();
        passwordEditText = dialog.getView(R.id.password);
        mCustomerKeyboard = dialog.getView(R.id.customer_keyboard);
        mCustomerKeyboard.setOnCustomerKeyboardClickListener(this);
        passwordEditText.setEnabled(false);
        passwordEditText.setOnPasswordFullListener(this);
    }
    /**
     * 打开支付弹窗
     * @param s 类型
     */
    private void showProductPop(final String s) {
        if (productInfo == null) {
            productInfo = new ProductInfoPop(this);
        }
        productInfo.setPopData(s, new ProductInfoPop.PayCallBack() {
            @Override
            public void pay(String type) {
                productInfo.dismiss();
                if (type.equals("余额")) {

                } else if (type.equals("微信")) {
                    ivHead.setImageResource(R.mipmap.weixinicon);
                    tvBalance.setVisibility(View.GONE);
                    tvBankname.setText("微信支付");
                } else if (type.equals("支付宝")) {
                    ivHead.setImageResource(R.mipmap.zhifubaoicon);
                    tvBalance.setVisibility(View.GONE);
                    tvBankname.setText("支付宝支付");
                } else {

                }

            }
        });
        productInfo.showAtLocation(this.findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置

    }
    @Override
    public void click(String numder) {
        passwordEditText.addPassword(numder);
    }

    @Override
    public void delete() {
        passwordEditText.deleteLastPassword();
    }

    @Override
    public void passwordFull(final String password) {
        /*验证支付密码*/
        TreeMap<String, String> treeMap = new TreeMap<String, String>() {
            {
                put("user_id", MyApp.getInstance().getUser_id());//用户id
                put("pay_password", password);//支付密码
            }
        };
        NetHelper.post(APIServer.VERIFYPAYPASSWORD, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                /*请求得到的数据*/
                JSONObject object = null;
                try {
                    object = new JSONObject(s);
                    if(object.getString("code").equals("200")) {
                        //成功
                        dialog.dismiss();
                    }else {
                        Toast.makeText(AccountRechargeActivity.this, "支付密码验证失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(AccountRechargeActivity.this,"服务器异常",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 充值
     */
    private void confirmPay() {
        TreeMap<String, String> treeMap = new TreeMap<String, String>() {
            {
                put("user_id", MyApp.getInstance().getUser_id());//用户id
                put("user_money", etMoney.getText().toString().trim());//充值额度
            }
        };
        NetHelper.post(APIServer.BALANCERECHARGE, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                WxPay.wxPay(AccountRechargeActivity.this, s);//微信支付
                finish();
            }
        });

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
